/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 * nlehms
 */
public class ClientTread extends Thread {

    private Socket socket;

    public ClientTread(Socket socket) {
        super("ClientTread");
        this.socket = socket;
        System.out.println("Connected to: "+socket.getInetAddress().getHostAddress());
    }

    public void run() {
        PrintWriter out;
        try {
            out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String inputLine;
            String outputLine;
            BatcherProtocol protocol = new BatcherProtocol(socket);
            out.println("username");
            while (protocol.state != -1) {
                try {
                    inputLine = in.readLine();

                    outputLine = protocol.processInput(inputLine);
                    out.println(outputLine);
                } catch (IOException ex) {
                    System.out.println("Connetion lost to: "+socket.getInetAddress().getHostAddress());
                    break;
                }
            }
            out.close();
            in.close();
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(ClientTread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
